<template>
  <div class="login-setting">
    <el-form
      ref="userForm"
      :model="userInfo"
      :rules="rules"
      label-width="120px"
      style="margin-left: 120px; margin-top: 30px"
    >
      <el-form-item label="姓名:" prop="username">
        <el-input v-model="userInfo.username" style="width:300px" />
      </el-form-item>

      <el-form-item label="密码:" prop="password">
        <el-input v-model="userInfo.password" type="password" style="width:300px" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="updateUser">更新</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getUserDetailByIdAPI, saveUserDetailByIdAPI } from '@/api'
export default {
  name: 'LoginSetting',
  data() {
    return {
      userInfo: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '用户名不能为空', triggler: 'blur' }],
        password: [{ required: true, message: '密码不能为空', triggler: 'blur' }]
      }
    }
  },
  created() {
    // 获取员工的详细信息
    this.getUserDetailFn()
  },
  methods: {
    // 获取员工的详细信息
    async getUserDetailFn() {
      try {
        // 1. 比如路由传参用/xxx/yyy?参数名=值&参数名=值
        // 到下一个页面想要拿到上一个页面传递过来的参数, this.$route.query.参数名
        // 2. 比如路由传参用/xxx/yyy/值
        // 前提: 路由规则对path:里/xxx/yyy/:参数名
        // 到下一个页面想要拿到上一个页面传递过来的参数, this.$route.params.参数名
        // 回顾vue基础路由那天传参的知识->写一写.
        const res = await getUserDetailByIdAPI(this.$route.query.id)
        console.log(res)
        this.userInfo.username = res.data.username
        this.userInfo.password = res.data.password
        this.userInfo.mobile = res.data.mobile
      } catch (err) {
        this.$message.error(err.message)
      }
    },
    // 更新->按钮点击事件
    updateUser() {
      this.$refs.userForm.validate(async valid => {
        if (valid) {
          const res = await saveUserDetailByIdAPI({
            ...this.userInfo,
            // username: '',
            // password: ''
            id: this.$route.query.id
          })
          console.log(res)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
